package com.keyroy.game.puzzle_rpg.models.environment;

import java.io.File;
import java.util.List;

import org.keyroy.util.tag.Tag;

import com.keyroy.game.puzzle_rpg.models.functions.FunctionSet;
import com.keyroy.game.puzzle_rpg.models.item.Item;
import com.keyroy.game.puzzle_rpg.models.item.ItemSet;
import com.keyroy.game.puzzle_rpg.models.puzzle.PuzzleSet;
import com.keyroy.game.puzzle_rpg.models.role.Role;
import com.keyroy.game.puzzle_rpg.models.role.RoleSet;
import com.keyroy.game.puzzle_rpg.models.scene.BattleScene;
import com.keyroy.game.puzzle_rpg.models.skill.Skill;
import com.keyroy.game.puzzle_rpg.models.skill.SkillFolder;
import com.keyroy.game.puzzle_rpg.models.skill.SkillSet;
import com.keyroy.game.puzzle_rpg.models.world.Map;
import com.keyroy.game.puzzle_rpg.models.world.WorldSet;
import com.keyroy.game.puzzle_rpg.utils.FileUtil;

public class Project {
	public static final String FOLDER_DATA = "data";

	private static Project _current_project;
	private File root;
	private WorldSet worldSet;
	private ItemSet itemSet;
	private PuzzleSet puzzleSet;
	private FunctionSet functionSet;
	private RoleSet roleSet;
	private SkillSet skillSet;

	public Project(File root) {
		this.root = root;
		checkFolder();
		load();
		_current_project = this;
	}

	private final void load() {
		loadItems();
		loadPuzzleSet();
		loadFunctionSet();
		loadRoleSet();
		loadSkillSet();
		loadWorldSet();
	}

	public void save() {
		FileUtil.save(getFile(ItemSet.class), getItemSet());
		FileUtil.save(getFile(PuzzleSet.class), getPuzzleSet());
		FileUtil.save(getFile(FunctionSet.class), getFunctionSet());
		FileUtil.save(getFile(RoleSet.class), getRoleSet());
		FileUtil.save(getFile(SkillSet.class), getSkillSet());
		FileUtil.save(getFile(WorldSet.class), getWorldSet());
	}

	private final void loadItems() {
		try {
			itemSet = (ItemSet) FileUtil.load(getFile(ItemSet.class), ItemSet.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final void loadPuzzleSet() {
		try {
			puzzleSet = (PuzzleSet) FileUtil.load(getFile(PuzzleSet.class), PuzzleSet.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final void loadFunctionSet() {
		try {
			functionSet = (FunctionSet) FileUtil.load(getFile(FunctionSet.class), FunctionSet.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final void loadRoleSet() {
		try {
			roleSet = (RoleSet) FileUtil.load(getFile(RoleSet.class), RoleSet.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final void loadSkillSet() {
		try {
			skillSet = (SkillSet) FileUtil.load(getFile(SkillSet.class), SkillSet.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final void loadWorldSet() {
		try {
			worldSet = (WorldSet) FileUtil.load(getFile(WorldSet.class), WorldSet.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ItemSet getItemSet() {
		if (itemSet == null) {
			itemSet = new ItemSet();
		}
		return itemSet;
	}

	public PuzzleSet getPuzzleSet() {
		if (puzzleSet == null) {
			puzzleSet = new PuzzleSet();
		}
		return puzzleSet;
	}

	public FunctionSet getFunctionSet() {
		if (functionSet == null) {
			functionSet = new FunctionSet();
		}
		return functionSet;
	}

	public RoleSet getRoleSet() {
		if (roleSet == null) {
			roleSet = new RoleSet();
		}
		return roleSet;
	}

	public SkillSet getSkillSet() {
		if (skillSet == null) {
			skillSet = new SkillSet();
		}
		return skillSet;
	}

	public WorldSet getWorldSet() {
		if (worldSet == null) {
			worldSet = new WorldSet();
		}
		return worldSet;
	}

	private final void checkFolder() {
		if (getDataFolder().exists() == false) {
			getDataFolder().mkdirs();
		}
	}

	public void print() {
		System.out.println(new Tag(getItemSet()).toFormateString());
		System.out.println(new Tag(getPuzzleSet()).toFormateString());
		System.out.println(new Tag(getFunctionSet()).toFormateString());
		System.out.println(new Tag(getRoleSet()).toFormateString());
		System.out.println(new Tag(getSkillSet()).toFormateString());
	}
	
	public boolean addMap(Map map){
		return getWorldSet().add(map);
	}

	public boolean addItem(Item item) {
		return getItemSet().add(item);
	}

	public void removeItem(Item item) {
		getItemSet().remove(item);
	}

	public void removeSkill(SkillFolder folder) {
		getSkillSet().remove(folder);
	}

	public void removeSkill(SkillFolder folder, Skill skill) {
		getSkillSet().remove(folder, skill);
	}

	public void init(Role role) {
		role.hp.value = functionSet.hpFunction.create(role);
		role.speed.value = functionSet.speedFunction.create(role);
	}

	public Battle createBattle(BattleScene scene, List<Role> allies, List<Role> enemies) {
		return Battle.create(scene, allies, enemies, puzzleSet);
	}

	public File getDataFolder() {
		return new File(root, FOLDER_DATA);
	}

	public File getFile(Object object) {
		return getFile(object.getClass());
	}

	public File getFile(Class<?> clazz) {
		return new File(getDataFolder(), clazz.getSimpleName() + ".xml");
	}

	public static Project getProject() {
		return _current_project;
	}

}
